#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"
#include "Serial.h"
#include "FlameSensor.h"
#include "key.h"
#include "sys.h"
#include "timer.h"

uint8_t red[3]={255,0,0};
uint8_t yellow[3]={255,255,0};
uint8_t green[3]={0,255,0};
uint8_t blue[3]={0,0,255};
uint8_t qin[3]={0,255,255};
uint8_t zi[3]={160,32,240};
uint8_t ziluolan[3]={138,43,226};
uint8_t hei[3]={0,0,0};	


// 打印信息，打印出火焰传感器的状态值
void makerobo_Print(int x)
{
	switch(x)
	{
		case 1:       // 安全
			printf("\r\n******************\r\n");
			printf("\r\n* Makerobo Saft~ *\r\n");
			printf("\r\n******************\r\n");
		break;
		case 0:      // 有火焰
			printf("\r\n******************\r\n");
			printf("\r\n Makerobo  Fire! *\r\n");
			printf("\r\n******************\r\n");
		break;
		default:    // 错误
			printf("\r\n**********************\r\n");
			printf("\r\n  Print value error. *\r\n");
			printf("\r\n**********************\r\n");
		break;
	}
}

int main(void)
{
	int makerobo_analogVal_voice;  //声音传感器AO模拟变量值
	OLED_Init();
	AD_Init();
	Serial_Init();
	OLED_ShowString(2,1,"Value:");
	
	
	int makerobo_analogVal;              // 模拟量变量
	int makerobo_tmp, makerobo_status;   // 模拟量状态值
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	
	OLED_Init();
	AD_Init();
	FlameSensor_Init();
	Serial_Init();
	//Delay_init();	    	  
	 TIM4_PWM_Init();

	makerobo_status = 0; 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	while(1)
	{	
		makerobo_analogVal = AD_GetValue();
		printf("Photoresistor Value: %d\n", makerobo_analogVal); //打印出该值 
			OLED_ShowString(1,1,"Value:");
		OLED_ShowNum(1,7,makerobo_analogVal,4);
		makerobo_tmp = FlameSensor_Get();
		if (makerobo_tmp != makerobo_status)       // 判断状态发生改变
		{	
			GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
		  Delay_ms(500);
			makerobo_Print(makerobo_tmp);         // 打印出火焰信息值
		makerobo_status = makerobo_tmp;       // 把当前状态值作为下次状态值判断，避免重复打印
		makerobo_led_Color_Set(red);
  	Delay_ms(500);
		makerobo_led_Color_Set(yellow);
		Delay_ms(500);
		makerobo_led_Color_Set(green);
		Delay_ms(500);
		makerobo_led_Color_Set(blue);
		Delay_ms(500);
		makerobo_led_Color_Set(qin);
		Delay_ms(500);
		makerobo_led_Color_Set(hei);
		Delay_ms(500);
		makerobo_led_Color_Set(ziluolan);
		Delay_ms(500);	
	}
    // 状态值
	GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
		Delay_ms(500);
	makerobo_analogVal_voice = AD_GetValue();
		printf("Photoresistor Value: %d\n", makerobo_analogVal); //打印出该值 
		OLED_ShowNum(2,7,makerobo_analogVal_voice,4);
 
		Delay_ms(200);
		
	}
}

